package com.yatuhashi.manual.mhp3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yatuhashi.manual.mhp3.bean.MonsterBean;
import com.yatuhashi.manual.mhp3.db.DataBaseHelper;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MonsterListActivity extends BaseActivity {
    private ExpandableListView mExpandableListView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.monster_list_activity);
        
        initHeader("モンスターリスト");
        
        initList();
    }
    
    private void initList2() {
        
    }
    
    private List<MonsterBean> parents;
    private List<MonsterBean> childs;
    protected void initList() {
        parents = getParents();
        childs = getChilds();
        
        List<Map<String, Object>> parentList = new ArrayList<Map<String,Object>>();
        List<List<Map<String, Object>>> allChildList = new ArrayList<List<Map<String,Object>>>();
        
        for(MonsterBean parent : parents) {
            Map<String, Object> parentData = new HashMap<String, Object>();
            parentData.put("RACE", parent.getRace());
            parentList.add(parentData);
            
            List<Map<String, Object>> childList = new ArrayList<Map<String,Object>>();
            for(MonsterBean child : childs) {
                if(child.getRid()==parent.getRid()) {
                    Map<String, Object> childData = new HashMap<String, Object>();
                    childData.put("NAME", child.getName());
                    childData.put("ALIAS", child.getAlias());
                    childData.put("WEAKNESS", child.getWeakness());
                    childList.add(childData);
                }
            }
            
            allChildList.add(childList);
        }
        
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                this,
                parentList,
                R.layout.row_expandablelist_parent,
                new String[]{"RACE"},
                new int[]{R.id.text1},
                allChildList,
                R.layout.row_monster_child,
                new String[] {"NAME", "ALIAS", "WEAKNESS"},
                new int[]{R.id.text1, R.id.text2, R.id.text3});
        
        mExpandableListView = (ExpandableListView) findViewById(R.id.list);
        mExpandableListView.setAdapter(adapter);
        
        mExpandableListView.setGroupIndicator(null);
        
        mExpandableListView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                ExpandableListAdapter adapter = parent.getExpandableListAdapter();
                
                @SuppressWarnings("unchecked")
                Map<String, Object> childMap = (Map<String, Object>) adapter.getChild(groupPosition, childPosition);
                goToMonsterDetail(childMap);
                return false;
            }
        });
    }
    
    private void goToMonsterDetail(Map<String, Object> childMap) {
        Toast.makeText(this, "その名も"+childMap.get("NAME")+"！", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.top_icon:
            finish();
            break;
        case R.id.app_title:
            for(int i=0; i<parents.size(); i++) {
                mExpandableListView.collapseGroup(i);
            }
            break;
        case R.id.left_btn:
            toast("search button");
            break;
        case R.id.right_btn:
            toast("share button");
            break;
        default:
            break;
        }
    }
    
    private List<MonsterBean> getParents() {
        DataBaseHelper dbHelper = new DataBaseHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        List<MonsterBean> list = new ArrayList<MonsterBean>();
        
        MonsterBean monster;
        String sql = "select name, alias, race, rid from monster_table";
        
        Cursor cur = db.rawQuery(sql, null);
        if(cur.moveToFirst()) {
            do {
                monster = new MonsterBean();
                monster.setName(cur.getString(cur.getColumnIndex("name")));
                monster.setAlias(cur.getString(cur.getColumnIndex("alias")));
                monster.setRace(cur.getString(cur.getColumnIndex("race")));
                monster.setRid(cur.getInt(cur.getColumnIndex("rid")));
                if(notContain(list, monster)) {
                    list.add(monster);
                }
            } while(cur.moveToNext());
        }
        cur.close();
        db.close();
        return list;
    }
    
    private boolean notContain(List<MonsterBean> list, MonsterBean monster) {
        for(MonsterBean bean : list) {
            if(monster.getRid()==bean.getRid()) {
                return false;
            }
        }
        return true;
    }
    
    private List<MonsterBean> getChilds() {
        DataBaseHelper dbHelper = new DataBaseHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        List<MonsterBean> list = new ArrayList<MonsterBean>();
        
        MonsterBean monster;
        String sql = "select name, alias, race, rid, weakness from monster_table";
        
        Cursor cur = db.rawQuery(sql, null);
        if(cur.moveToFirst()) {
            do {
                monster = new MonsterBean();
                monster.setName(cur.getString(cur.getColumnIndex("name")));
                monster.setAlias(cur.getString(cur.getColumnIndex("alias")));
                monster.setRace(cur.getString(cur.getColumnIndex("race")));
                monster.setRid(cur.getInt(cur.getColumnIndex("rid")));
                monster.setWeakness(cur.getString(cur.getColumnIndex("weakness")));
                list.add(monster);
            } while(cur.moveToNext());
        }
        cur.close();
        db.close();
        return list;
    }
}
